<?php
namespace module\tv\controller;

use think\Request;
use core\tv\model\StoreModel;
use core\tv\model\StoreGroupModel;
use core\tv\validate\StoreGroupValidate;
use core\tv\logic\StoreGroupLogic;

class StoreGroup extends Base
{

    /**
     * 分组列表
     *
     * @param Request $request            
     * @return string
     */
    public function index(Request $request)
    {
        $this->siteTitle = '门店分组';
        
        // 记录列表
        $list = StoreGroupModel::getInstance()->getStoreGroupList();
        $this->_list($list);
        
        return $this->fetch();
    }

    /**
     * 添加分组
     *
     * @param Request $request            
     * @return mixed
     */
    public function add(Request $request)
    {
        if ($request->isPost()) {
            $data = [
                'group_name' => $request->param('group_name'),
                'videos' => implode(',',$request->param('videos/a', [])),
                'sort' => $request->param('sort', 0)
            ];

            // 验证
            $this->_validate(StoreGroupValidate::class, $data, 'add');
            
            // 添加
            $this->_add(StoreGroupModel::class, $data);
        } else {
            $this->siteTitle = '新增分组';

            // 选择列表
            $videoList = $videoList = StoreGroupLogic::getInstance()->getSelectList();
            $this->assign('videoList', $videoList);
            return $this->fetch();
        }
    }

    /**
     * 编辑分组
     * 
     * @param Request $request            
     * @return mixed
     */
    public function edit(Request $request)
    {
        if ($request->isPost()) {
            $data = [
                'group_name' => $request->param('group_name'),
                'videos' => implode(',',$request->param('videos/a', [])),
                'sort' => $request->param('sort', 0)
            ];
            
            // 验证
            $this->_validate(StoreGroupValidate::class, $data, 'edit');
            
            // 保存
            $this->_edit(StoreGroupModel::class, $data);
        } else {
            $this->siteTitle = '编辑分组';
            
            // 记录
            $this->_record(StoreGroupModel::class);

            // 选择列表
            $videoList = StoreGroupLogic::getInstance()->getSelectList();
            $this->assign('videoList', $videoList);
            
            return $this->fetch();
        }
    }

    /**
     * 分组排序
     * 
     * @return void
     */
    public function sort()
    {
        $this->_sort(StoreGroupModel::class, 'sort');
    }

    /**
     * 修改字段值
     *
     * @param Request $request            
     * @return mixed
     */
    public function modify(Request $request)
    {
        $fields = [
            'sort',
        ];
        $this->_modify(StoreGroupModel::class, $fields);
    }

    /**
     * 删除群组
     * 
     * @return void
     */
    public function delete()
    {
        $model = StoreModel::getInstance();
        $map = [
            'group_id' => $this->_id()
        ];
        if ($model->where($map)->find()) {
            $this->error('正在使用的分组不可删除');
        }
        
        $this->_delete(StoreGroupModel::class, false);
    }

}